/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zwu.t1;

import java.io.File;
import javax.jcr.*;
import javax.jcr.nodetype.*;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.jackrabbit.core.TransientRepository;

/**
 *
 * @author Administrator
 */
public class DocQuery {
public static void main(String[] args) throws Exception {

        System.out.println(new File(".").getAbsolutePath());
        Repository repository = new TransientRepository(new File("data/t2"));
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));
        try {
            Node root = session.getRootNode();

           testQuery(session.getWorkspace());

        } finally {
            session.logout();
        }
    }


   public static void testQuery(Workspace wk) throws Exception
    {


          QueryManager qm = wk.getQueryManager();
            //String query = "select excerpt(.) from nt:resource where contains(., 'us')";
            String query = "select * from [nt:resource] as file where contains(file.*, 'nice')";

            Query q = qm.createQuery(query, Query.JCR_SQL2);
            QueryResult result = q.execute();
            for (RowIterator it = result.getRows(); it.hasNext();) {
                Row r = it.nextRow();

                Node nn = r.getNode().getParent();
                System.out.println(nn.getPath() + nn.getName());
                PropertyIterator pi2 = nn.getProperties();
                while (pi2.hasNext()) {
                    System.out.println(pi2.nextProperty().getName());
                }

                //Value excerpt = r.getNode("file").g("rep:excerpt(file)");
                //System.out.print(excerpt.getString());
            }



    }

}
